package cn.wolfcode.restful.web.controller;


import cn.wolfcode.restful.domain.Employee;
import cn.wolfcode.restful.domain.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@RequestMapping("employees")
public class EmployeeController02 {

    /*  1.获取所有的员工(集合)
      1.资源路径设计 /employees
      2.请求动作设计 get
      3.请求参数设计 无
      4.返回结果设计 List<Employee>*/


   @GetMapping
    public Object list() {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee(1L, "图南",22));
        list.add(new Employee(2L, "一针",33));
        return list;
    }
   /* 1.获取一个员工
      1.资源路径设计 /employees
      2.请求动作设计 get
      3.请求参数设计 id
      4.返回结果设计 Employee*/


    @GetMapping(value = "/{id}")
    public Object getOne(@PathVariable Long id) {

        Employee employee = new Employee(id, "带筋", 22);
        return employee;
    }

    /*  1.增加一个员工
      1.资源路径设计 /employees
      2.请求动作设计 post
      3.请求参数设计 name  age
      4.返回结果设计 Employee*/

   @PostMapping
    public Object getSave(Employee employee) {

        Employee e = new Employee(1L, employee.getName(), employee.getAge());
        return e;
    }
    /*  1.更新一个员工
      1.资源路径设计 /employees
      2.请求动作设计 put
      3.请求参数设计 Employee
      4.返回结果设计 Employee*/

    @PutMapping
    public Object update(Employee employee) {

        Employee e = new Employee(1L, "小吴", 22);
             e.setName(employee.getName());
             e.setAge(employee.getAge());
             e.setId(employee.getId());
        return e;
    }

    /*  1.删除一个员工
      1.资源路径设计 /employees
      2.请求动作设计 DELETE
      3.请求参数设计 id
      4.返回结果设计 JsonResult*/

    @DeleteMapping(value = "/{id}")
    public Object delete(@PathVariable Long id) {

        Employee e = new Employee(1L, "小吴", 22);
        if (id.equals(e.getId())){
            return new  JsonResult().getMsg();
        }

        return new JsonResult(false,"失败");
    }












}
